﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace library
{
    public partial class purchase : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        public purchase()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select bname as 书名,author as 作者,press as 出版社,pub_time as 出版时间,bought_status as 荐购状态,note as 处理备注 from pur_info";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sd.Fill(ds, "pur_info");
            dataGridView3.DataSource = ds;
            dataGridView3.DataMember = "pur_info";
            conn.Close();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            conn.Open();
            string ISBN = textBox2.Text;
            string uname = User.uname;
            string pur_time = dateTimePicker3.Text;
            string bname = textBox9.Text;
            string author = textBox10.Text;
            string press = textBox11.Text;
            string pub_time = dateTimePicker1.Text;
            string rec_reason = textBox3.Text;
            if (ISBN.Equals("") || bname.Equals("") || author.Equals(""))
            {
                MessageBox.Show("ISBN或书名或作者不能为空！");
            }
            else
            {
                /*string sql3 = "select lose_whether='否' from book_info where ISBN='" + ISBN + "'";
                MySqlCommand cmd3 = new MySqlCommand(sql3, conn);
                cmd3.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                MySqlDataReader sdr3 = cmd3.ExecuteReader();
                if (sdr3.HasRows)
                {
                    MessageBox.Show("荐购失败，该书在图书馆已有馆藏");
                }
                else
                {*/
                    //sdr3.Close();
                    string sql1 = "select * from book1_info where ISBN ='" + ISBN + "' ";
                    MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
                    cmd1.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                    MySqlDataReader sdr = cmd1.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        MessageBox.Show("您所荐购的书在图书馆已有收藏，荐购失败");
                    }
                    else
                    {
                        sdr.Close();
                        string sql2 = "select * from pur_info where ISBN = '" + ISBN + "' and bname='" + bname + "'";
                        MySqlCommand cmd2 = new MySqlCommand(sql2, conn);
                        cmd2.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                        cmd2.Parameters.Add("@bname", MySqlDbType.VarChar).Value = bname;
                        MySqlDataReader sdr1 = cmd2.ExecuteReader();
                        if (sdr1.HasRows)
                        {
                            MessageBox.Show("您所荐购的书已被其他人所荐购，信息重合，荐购失败！");
                            MessageBox.Show("如有需要请重新荐购");
                        }
                        else
                        {
                            sdr1.Close();
                            string sql = "insert into pur_info(ISBN,uname,pur_time,bname,author,press,pub_time,rec_reason) values ('" + ISBN + "','" + uname + "','" + pur_time + "','" + bname + "','" + author + "','" + press + "','" + pub_time + "','" + rec_reason + "')";
                            MySqlCommand cmd = new MySqlCommand(sql, conn);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("提交成功");
                        }
                        sdr1.Close();
                    }
                    sdr.Close();
                //sdr3.Close();
            }
            conn.Close();
        }

        private void purchase_Load(object sender, EventArgs e)
        {
            show_name();
        }
        private void show_name()
        {
            label21.Text = User.uname;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string uname = User.uname;
            conn.Open();
            string sql = "select bname as 书名,author as 作者,press as 出版社,pub_time as 出版时间,bought_status as 荐购状态,note as 处理备注 from pur_info where uname=@uname";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            cmd.Parameters.Add("@uname", MySqlDbType.VarChar).Value = uname;
            DataSet ds = new DataSet();
            sd.Fill(ds, "pur_info");
            dataGridView3.DataSource = ds;
            dataGridView3.DataMember = "pur_info";
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox9.Clear();
            textBox10.Clear();
            textBox11.Clear();
            textBox3.Clear();
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
